<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Models\CloudimageType;
use App\Http\Controllers\Controller;
class CloudimageTypeController extends Controller
{
    protected $model;

    public function __construct(CloudimageType $cloudimageType)
    {
        $this->model = $cloudimageType;
    }

    /**
     * Display a listing of the resource.
     * @return \Illuminate\Http\Response
     * 分类列表
     */

    public function index(Request $request)
    {
        $result = $this->model->filter($request->all())->orderBy('created_at', 'desc')->paginate(config('api.page.limit'));
        return view('admins.cloudimagetype.index')->with('result', $result);
    }
	/*
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request,CloudimageType $cloudimagetype)
    {
        $result = $cloudimagetype->create($request->all());
        if(!$result->id){
            return back()->with('warning','添加失败');
         }else{
             return redirect()->route('admin.cloudimagetype.index')->with('warning','添加成功！');
         }
        
    }

    public function create(Request $request)
    {
        $categs = CloudimageType::getcates();
        return view('admins.cloudimagetype.add')->with("categs", $categs);

    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @return \Illuminate\Http\Response
     */

    public function edit($id)
    {
        //
        $result = CloudimageType::find($id);
        return view('admins.cloudimagetype.edit', compact('result'));  
    }

    /**
     * Update the specified resource in storage.
     *
     * @param \Illuminate\Http\Request $request
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request)
    {
        
        $result = CloudimageType::find($request->input('id'));
        $result->cloudimagesname = $request->input('cloudimagesname');
        if($result->save()){
            return redirect()->route('admin.cloudimagetype.index')->with('success', '编辑成功');
        }else{
            return back()->with('warning','编辑失败');
        }
    }

    public function destroy($id)
    {
        if ($id) {
            $result = $this->model ->where('id', $id)->update(['state' => 0]);
        }
      return redirect()->route('admin.cloudimagetype.index');
    }
}
